package com.tcm.doctor.web.controller;

import com.base.core.utils.R;
import com.tcm.item.domain.dto.CategoryDTO;
import com.tcm.item.service.category.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(value = "category", tags = "药品分类接口")
@RequestMapping("/doctor")
@RestController
public class CategoryController {

    @DubboReference(version = "0.0.1-alpha", timeout = 3000, check = false, retries = 3)
    private CategoryService categoryService;

    @GetMapping("/category/tree")
    public R tree() throws Exception {
        return R.success(categoryService.listToTree());
    }

    @PostMapping("/category/save")
    public R insert(@RequestBody CategoryDTO category) {
        try{
            categoryService.createCategory(category);
        }catch (Exception e){
            R.fail(e.getMessage());
        }
        return R.success();
    }

    @PostMapping("/category/update")
    public R update(@RequestBody CategoryDTO category) {
        int result = categoryService.updateCategory(category);
        if(result > 0){
            return R.success();
        }
        return R.fail("更新失败");
    }

    @GetMapping("/category/delete")
    public R delete(@RequestParam("id") Long id) {
        int result = categoryService.deleteCategory(id);
        if(result > 0){
            return R.success();
        }
        return R.fail("删除失败");
    }
}
